/**
 * Copyright (c) 2018 人人开源 All rights reserved.
 * <p>
 * https://www.renren.io
 * <p>
 * 版权所有，侵权必究！
 */

package io.fft.flight.common.exception;


import io.fft.flight.common.utils.MessageUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;

/**
 * 自定义异常
 *
 * @author Mark sunlightcs@gmail.com
 */
public class RenException extends RuntimeException {
    private static final long serialVersionUID = 1L;

    /**
     * 错误信息
     */
    protected IErrorInfo errorInfo;

    /**
     * 异常消息参数
     */
    protected Object[] args;

    public RenException(String message){
        super(message);
    }

    public RenException(IErrorInfo errorInfo) {
        super(errorInfo.getMessage());
        this.errorInfo = errorInfo;
    }

    public RenException(int code, String msg) {
        super(msg);
        this.errorInfo = new IErrorInfo() {
            @Override
            public Integer getCode() {
                return code;
            }

            @Override
            public String getMessage() {
                return msg;
            }
        };
    }
    public RenException(int code, String... msg) {
        this.errorInfo = new IErrorInfo() {
            @Override
            public Integer getCode() {
                return code;
            }

            @Override
            public String getMessage() {
                StringBuilder sb=new StringBuilder();
                if(Objects.nonNull(msg)){
                    for (String s : msg) {
                        sb.append(s).append(",");
                    }
                }
                return sb.toString();
            }
        };
    }
    public RenException(int code, Throwable cause) {
        this(code,cause,null);
    }
    public RenException(int code, Throwable cause,String message) {
        super(message,cause);
        this.errorInfo = new IErrorInfo() {
            @Override
            public Integer getCode() {
                return code;
            }

            @Override
            public String getMessage() {
                return StringUtils.isNotEmpty(message)?message: "系统异常！";
            }
        };
    }
    public RenException(IErrorInfo errorInfo, Object[] args, String message) {
        super(message);
        this.errorInfo = errorInfo;
        this.args = args;
    }

    public RenException(IErrorInfo errorInfo, Object[] args, String message, Throwable cause) {
        super(message, cause);
        this.errorInfo = errorInfo;
        this.args = args;
    }

    public IErrorInfo getErrorInfo(){
        return this.errorInfo;
    }
}